package cn.dnui_wnx725.dnuifood_16110100725.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

import java.util.List;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.adapter.FoodByShopAdapter;
import cn.dnui_wnx725.dnuifood_16110100725.bean.FoodBean;
import cn.dnui_wnx725.dnuifood_16110100725.controller.FoodDetailActivity;
import cn.dnui_wnx725.dnuifood_16110100725.listener.ListListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.SearchModel;


/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFragment extends BaseFragment {
    private EditText et_search;
    private RecyclerView tv_search;
    private ImageButton btn_search;
    private RecyclerView.LayoutManager linearLayoutManager;
    private ListListener<FoodBean> listListener = new ListListener<FoodBean>() {
        @Override
        public void onResponse(final List<FoodBean> list) {
            FoodByShopAdapter foodByShopAdapter = new FoodByShopAdapter(getActivity(),list, R.layout.item);
            FoodByShopAdapter.OnItemClickListener onItemClickListener = new FoodByShopAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent =new Intent(getActivity(), FoodDetailActivity.class);
                    intent.putExtra("foodname",list.get(position).getFoodname());
                    intent.putExtra("foodintro",list.get(position).getIntro());
                    intent.putExtra("foodprice",list.get(position).getPrice());
                    intent.putExtra("foodimg",list.get(position).getPic());
                    intent.putExtra("foodid",list.get(position).getFood_id());
                    startActivity(intent);
                }
            };
            foodByShopAdapter.setList(list);
            foodByShopAdapter.setmOnItemClickListener(onItemClickListener);
            tv_search.setLayoutManager(linearLayoutManager);
            tv_search.setHasFixedSize(true);
            tv_search.setAdapter(foodByShopAdapter);

        }

        @Override
        public void onFail(String msg) {

        }
    };


    @Override
    void initView() {
        linearLayoutManager =new LinearLayoutManager(getActivity());
        et_search=view.findViewById(R.id.et_search);
        tv_search=view.findViewById(R.id.rv_searchlist);
        btn_search=view.findViewById(R.id.btn_search);
    }

    @Override
    void initEvent() {
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchModel searchModel = new SearchModel();
                searchModel.getFoodMessage(et_search.getText().toString(),listListener);
            }
        });
    }

    @Override
    void initData() {

    }

    public SearchFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_search);
        return super.onCreateView(inflater,container,savedInstanceState);
    }

    private void request_list(){
        SearchModel searchModel = new SearchModel();
        searchModel.getFoodMessage(et_search.getText().toString(),listListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        request_list();
    }
}